package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Turnover;
import com.ruoyi.system.service.ITurnoverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成交量Controller
 *
 * @author ruoyi
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/system/turnover")
public class TurnoverController extends BaseController {
    @Autowired
    private ITurnoverService turnoverService;

    /**
     * 查询成交量列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Turnover turnover) {
        startPage();
        List<Turnover> list = turnoverService.selectTurnoverList(turnover);
        return getDataTable(list);
    }

    @GetMapping("/info")
    public AjaxResult info(){
        Turnover turnover = new Turnover();
        List<Turnover> list = turnoverService.selectTurnoverList(turnover);
        List<String> title = new ArrayList<>();
        List<BigDecimal> num = new ArrayList<>();
        for (Turnover turnover1 : list) {
            title.add(turnover1.getTitle());
            num.add(turnover1.getNum());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("title",title);
        map.put("num",num);
        return AjaxResult.success(map);
    }

    /**
     * 获取成交量详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(turnoverService.selectTurnoverById(id));
    }

    /**
     * 新增成交量
     */
    @PostMapping
    public AjaxResult add(@RequestBody Turnover turnover) {
        Turnover res = new Turnover();
        res.setTitle(turnover.getTitle());
        List<Turnover> list = turnoverService.selectTurnoverList(res);
        Assert.isTrue(list.size()==0,"该标题已有数据");
        turnover.setCreateUser(SecurityUtils.getUserId().intValue());
        return toAjax(turnoverService.insertTurnover(turnover));
    }

    /**
     * 修改成交量
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Turnover turnover) {
        turnover.setUpdateUser(SecurityUtils.getUserId().intValue());
        return toAjax(turnoverService.updateTurnover(turnover));
    }

    /**
     * 删除成交量
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(turnoverService.deleteTurnoverByIds(ids));
    }
}
